
#include "gameApp.h" 
#include "time.h"
#import "MyView.h"
#import <UIKit/UIKit.h>
#import <Foundation/NSCalendar.h>
#import <OpenGLES/ES1/gl.h>

#ifndef NO_SOUND
static UInt32    SoundId[MAX_SOUNDID];
static char      MusicPath[256]={0};
static boolean   MusicLoop=FALSE;
static float     Volume_bg = 0.5;
static float     Volume_se = 0.5;
#endif

CRandGen*			CGameApp::m_pRandGen;
CTouchInput*		CGameApp::m_pTouchInput;
CGameApp*			CGameApp::m_pGameApp;
CGameScene *        CGameApp::m_pGameScreen;
CGameControll*      CGameApp::m_pControllData; 
void * CGameApp::m_pView;

CGameApp::CGameApp()
{	
}

CGameApp::~CGameApp()
{
}

void CGameApp::OnInit(void * pView)
{
	m_pView = pView;
	m_pGameApp = new CGameApp;
	m_pRandGen = new CRandGen;
	m_pTouchInput = new CTouchInput;
	m_pGameScreen = new CGameScene();
	m_pControllData = NULL;
	InitSound();
}

void CGameApp::OnFree() 
{
	SAFE_DELETE( m_pGameScreen );
	SAFE_DELETE( m_pTouchInput );
	SAFE_DELETE( m_pRandGen );
	SAFE_DELETE( m_pGameApp );
}

void CGameApp::HandleUpdate(int timeElapsedMS)
{
//	m_pGameScreen->HandleUpdate(timeElapsedMS);
}

void CGameApp::HandleRender()
{
//	m_pGameScreen->HandleRender();
}

void CGameApp::SetControllData(CGameControll * conData)
{
	if(!m_pControllData && conData)
	{
		m_pControllData = conData;
		[(MyView*)m_pView AddControll];
	}
}

CGameControll* CGameApp::GetControllData()
{
	return m_pControllData;
}

void CGameApp::ControllCallBack()
{
//	m_pGameScreen->m_CurPage->ControllCallBack(m_pControllData);
}

void CGameApp::DeleteControll()
{
	if(m_pControllData)
	{
		[(MyView*)m_pView DelControll];
		m_pControllData = NULL;
	}
}

void CGameApp::InitSound()
{
#ifndef NO_SOUND
	//Setup sound engine - We run at 44Khz to match our sound files
	CALL_SOUNDENGINE_FUNCTION(SoundEngine_Initialize, 24000);
	CALL_SOUNDENGINE_FUNCTION(SoundEngine_SetListenerPosition, 0.0, 0.0, 1.0);
	
	m_pGameScreen->InitGameSound();
	
	SetVolume(Volume_bg,Volume_se);
	if(MusicPath[0])PlayMusic(MusicPath,MusicLoop);
#endif
}

void CGameApp::SetVolume(float volume_bg,float volume_se)
{
#ifndef NO_SOUND
	Volume_bg=volume_bg;
	Volume_se=volume_se;
    SoundEngine_SetEffectsVolume(volume_se);
    SoundEngine_SetBackgroundMusicVolume(volume_bg);
#endif
}

void CGameApp::PlayMusic( const char *path, boolean loop )
{
#ifndef NO_SOUND
    MusicLoop=loop;
	if( path==NULL )
	{
		MEMSET(MusicPath,0,256);
		SoundEngine_StopBackgroundMusic( FALSE );
	}
	else
	{
		NSString * sFileName = [[NSString alloc] initWithCString:path];
		MEMSET(MusicPath,0,256);
		MEMCPY(MusicPath,path,[sFileName length]);

		SoundEngine_UnloadBackgroundMusicTrack();
		SoundEngine_LoadBackgroundMusicTrack( [[[NSBundle mainBundle] pathForResource:sFileName ofType:nil] UTF8String], FALSE, FALSE );
		SoundEngine_StartBackgroundMusic();
	
		if (!MusicLoop)SoundEngine_StopBackgroundMusic(TRUE);
		
		SoundEngine_SetBackgroundMusicVolume( Volume_bg );
		[sFileName release];
	}
#endif //#ifndef NO_SOUND
}

void CGameApp::LoadSound( const char *path, int id)
{
#ifndef NO_SOUND
	NSString * sFileName = [[NSString alloc] initWithCString:path];
	CALL_SOUNDENGINE_FUNCTION(SoundEngine_LoadEffect, [[[NSBundle mainBundle] pathForResource:sFileName ofType:nil] UTF8String], &SoundId[id]);
	[sFileName release];
#endif
}

void CGameApp::PlaySound( int id )
{
#ifndef NO_SOUND
	CALL_SOUNDENGINE_FUNCTION(SoundEngine_StartEffect, SoundId[id]);
#endif
}

bool CGameApp::IsPlayingMusic()
{
#ifndef NO_SOUND
	if(MusicPath[0])return true;
#endif
	return false;
}

void CGameApp::SetGameInterrupt(boolean interrupt)
{
#ifndef NO_SOUND
    if(interrupt)SoundEngine_SetBackgroundMusicVolume(0);
	else SoundEngine_SetBackgroundMusicVolume(Volume_bg);
#endif
}

void CGameApp::GameAppInterruptionListener( void *inUserData, int32 interruptionState )
{
#ifndef NO_SOUND
	if(interruptionState == kAudioSessionBeginInterruption) 
	{
		// deactivate our audio session
		AudioSessionSetActive( FALSE );
		
		// tear down the sound engine
		CALL_SOUNDENGINE_FUNCTION(SoundEngine_Teardown);
	}
	else if( interruptionState == kAudioSessionEndInterruption ) 
	{
		// activate our audio session
		AudioSessionSetActive( TRUE );
		InitSound();
	}
#endif
}

void CGameApp::InitPerspective(GLfloat fovy, GLfloat aspect, GLfloat zNear, GLfloat zFar)
{
    GLfloat xmin, xmax, ymin, ymax;
	
    ymax = zNear * (GLfloat)tan(fovy * 3.1415962f / 360.0);
    ymin = -ymax;
    xmin = ymin * aspect;
    xmax = ymax * aspect;
	
    glFrustumf(xmin, xmax, ymin, ymax, zNear, zFar);
}

void CGameApp::Enable3D()
{
	int width = ScreenWidth();
	int height = ScreenHeight();
	float aspect = (float)width/(float)height;
	
	glViewport          (0, 0, width, height);
    glScissor           (0, 0, width, height);
	
    glMatrixMode        (GL_PROJECTION);
    glLoadIdentity      ();
    InitPerspective     (60.f, aspect, 0.1f, 10000.f);    
    glMatrixMode        (GL_MODELVIEW);
    glLoadIdentity      ();
    
	glCullFace          (GL_FRONT);
	glEnable            (GL_CULL_FACE);
	glEnable			(GL_DEPTH_TEST);	
	
	glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
	glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
}

void CGameApp::Enable2D()
{
	int width = ScreenWidth();
	int height = ScreenHeight();
	
	glViewport      (0, 0, width, height);
	glScissor       (0, 0, width, height);
	
	glMatrixMode	(GL_PROJECTION);
    glLoadIdentity  ();
	glOrthof(0, (float)width, (float)height, 0,  -100, 100);
    glMatrixMode    (GL_MODELVIEW);
	glLoadIdentity();
	
	glDisable				(GL_CULL_FACE);
	glDisable				(GL_DEPTH_TEST);
}
